package wangjg.coco.actions;


import java.io.File;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import wangjg.coco.Activator;
import wangjg.coco.common.FileExplorer;
import wangjg.coco.views.ExplorerPreferencePage;
/**
 * 通过os打开对应的资源
 * @author wangjigang1
 *
 */
public class ExplorerPopupMenuAction implements IObjectActionDelegate {
 
	private Shell shell;
	
	/**
	 * Constructor for Action1.
	 */
	public ExplorerPopupMenuAction() {
		super();
	}
 
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.shell = targetPart.getSite().getShell();
	}
 
	public void run(IAction action) {
		if(selectedElement!=null){
			if(selectedElement instanceof IResource){
				System.out.println("selectedElement="+selectedElement.getClass().getName());
				File file= ((IResource) selectedElement).getLocation().toFile();
				boolean useCmd = ExplorerPreferencePage.useCmd();
				String cmd = ExplorerPreferencePage.getCmd();
				new FileExplorer(useCmd, cmd).open(file);
			}
		}
	}
	
	private  Object selectedElement;
 
	public void selectionChanged(IAction action, ISelection selection) {
		if(selection instanceof  IStructuredSelection){
			IStructuredSelection ss = (IStructuredSelection) selection;
		    selectedElement = ss.getFirstElement();
		}
//		Shell dialog = new Shell(SWT.DIALOG_TRIM);
//		dialog.open();
	} 
 
}